//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreNuke.StarterKit.Commerce
{
	/// <summary>
	/// Summary description for CommerceDefault.
	/// </summary>
	public class CommerceDefault : Page
	{
		public string title = "";
		public string description = "";
		public string keyWords = "";
		public string copyright = "";
		public string generator = "";
		public string author = "";

		protected Label SkinError;
		protected PlaceHolder CSS;
		protected PlaceHolder SkinPlaceHolder;
		protected System.Web.UI.HtmlControls.HtmlContainerControl Body;
		protected System.Web.UI.HtmlControls.HtmlInputHidden ScrollTop;
	
		private void Page_Load(object sender, EventArgs e)
		{
			if(ScrollTop.Value != "") {
				Body.Attributes.Add("onload", "javascript:Body.scrollTop=" + ScrollTop.Value + ";");
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			
			//load skin control
            UserControl ctlSkin = new UserControl();
			SkinDB skin = new SkinDB();
			string skinPath = skin.GetSkinPath();

			//Load Skin "Selected Skin Path"
			if(skinPath != "")
			{
				ctlSkin = LoadSkin(skin.GetSkinPath());
			}
			else
			{
				ctlSkin = ReturnDefaltSkin();
			}

			//Load default skin if error ocurs
			if (ctlSkin == null)
			{
				ctlSkin = ReturnDefaltSkin();;
			}

			//Add skin to page
			SkinPlaceHolder.Controls.Add(ctlSkin);

			//Load Meta tags
			LoadMetatags();
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		private UserControl LoadSkin(string SkinPath) 
		{ 
			UserControl ctlSkin = new UserControl(); 
			try 
			{ 
				ctlSkin = ((UserControl)(LoadControl(SkinPath))); 
			} 
			catch
			{
				try
				{
					ctlSkin = ((UserControl)(LoadControl(ConfigurationSettings.AppSettings["defaultSkin"]))); 
				}			
				catch (Exception ex) 
				{ 
					SkinError.Text += "<center>Could Not Load Skin: " + SkinPath + " Error: " + Server.HtmlEncode(ex.Message) + "</center><br>"; 
					SkinError.Visible = true; 
				} 
			}
			return ctlSkin; 
		}

		private UserControl ReturnDefaltSkin()
		{
			return LoadSkin(ConfigurationSettings.AppSettings["defaultSkin"]);
		}

		//This will most likely because an overritten set of variables
		private void LoadMetatags()
		{
			title = "";
			description = "";
			keyWords = "";
			copyright = "";
			generator = "";
			author = "";
		}
	}
}
